package com.music.mysongs.service

import android.app.*
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Binder
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi
import com.music.mysongs.helper.NotifyHelper
import com.music.mysongs.helper.PlayHelper
import com.music.mysongs.utils.myLog

@RequiresApi(Build.VERSION_CODES.M)
class MusicService : Service() {
    private val mTAG = "MusicService"

    private var myPlayer = PlayHelper(this@MusicService)
    private val myBinder = MusicBinder()
    private val myNotify = NotifyHelper(this@MusicService)
    private val myReceiver = MusicReceiver()

    inner class MusicReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            when (intent.action) {
                "com.music.mysongs.LAST" -> {
                    myPlayer.playLast()
                }
                "com.music.mysongs.PLAY" -> {
                    myPlayer.playOrPause()
                }
                "com.music.mysongs.NEXT" -> {
                    myPlayer.playNext()
                }
                else -> {
                    myLog("$mTAG: action=${intent.action}")
                    //myGoActivity(context, MainActivity::class.java)
                }
            }
            myLog("$mTAG: onReceiver")
            //myNotify.stopNotify()
            myNotify.initNotification(
                myPlayer.getTitle(),
                myPlayer.getArtist(),
                myPlayer.getIsPlay()
            )
        }
    }

    inner class MusicBinder : Binder() {
        @RequiresApi(Build.VERSION_CODES.M)
        fun setNewInit(lik: String, pos: Int) {
            myPlayer.initPlay(lik, pos)
        }

        //传给binder歌名
        fun setName(): String {
            return myPlayer.getTitle()
        }

        //传给binder歌手名
        fun setArtist(): String {
            return myPlayer.getArtist()
        }

        //传给binder进度
        fun setCurrent(): Int {
            return myPlayer.getCurrent()
        }

        //传给binder时长
        fun setDuration(): Int {
            return myPlayer.getDuration()
        }

//        private fun setSecSeek(): Int {
//            return myPlayer.getSecSeek()
//        }

        //service接收切换
        fun setLast() {
            myPlayer.playLast()
        }

        fun setNext() {
            myPlayer.playNext()
        }

        //service接收进度
        fun setSeekTo(po: Int) {
            myPlayer.playPull(po)
        }

        //service接收播放
        fun setIsPlay(): Boolean {
            return myPlayer.getIsPlay()
        }

        fun setPlayOrPause() {
            myPlayer.playOrPause()
        }

        //service接收播放模式
        fun setIsOrder(): Boolean {
            return myPlayer.getIsOrder()
        }

        fun setOrder() {
            myPlayer.setPlayOrder()
        }

        //service接收是否喜欢
        fun setIsLike(): Boolean {
            return myPlayer.getIsLove()
        }

        fun setLike() {
            myPlayer.setLove()
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onBind(intent: Intent?): IBinder {
        myLog("$mTAG: onBind")
        val lik = intent?.getStringExtra("sorts")
        val pos = intent?.getIntExtra("index", -1)
        myLog("$mTAG: lik=$lik pos=$pos")
        myPlayer.initPlay(lik, pos)
        //注册广播
        val filter = IntentFilter()
        filter.addAction("com.music.mysongs.LAST")
        filter.addAction("com.music.mysongs.PLAY")
        filter.addAction("com.music.mysongs.NEXT")
        registerReceiver(myReceiver, filter)
        //开启通知
        myNotify.initNotification(myPlayer.getTitle(), myPlayer.getArtist(), true)
        return myBinder
    }

    override fun onUnbind(intent: Intent?): Boolean {
        myLog("$mTAG: onUnbind")
        myPlayer.playStop()
        unregisterReceiver(myReceiver)
        myNotify.stopNotify()
        return super.onUnbind(intent)
    }

    override fun onDestroy() {
        myLog("$mTAG: onDestroy")
        super.onDestroy()
    }
}